package com.journaldev.spring;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class PagesInterceptor extends HandlerInterceptorAdapter {
	private List<String> nonAuthPaths;
	
	public PagesInterceptor() {
		
		nonAuthPaths = new ArrayList<String>();
	    nonAuthPaths.add("/login");
	    nonAuthPaths.add("/login/authenticate");
	    //nonAuthPaths.add("/resources/vendor/fonts/glyphicons-halflings-regular.woff");
	    //nonAuthPaths.add("/resources/vendor/fonts/glyphicons-halflings-regular.ttf");
	}

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		// TODO Auto-generated method stub
		Object user = request.getSession().getAttribute("USER_LOGIN");

	    if (user == null && !nonAuthPaths.contains(request.getServletPath())) {
	            response.sendRedirect(request.getContextPath() + "/login");
		        return false;
        } else {
	        return true;
	    }
	}
	
	
}
